﻿using System;

namespace 简单工厂
{
    public class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("请输入数字A");
                string numberA = Console.ReadLine();
                Console.WriteLine("请选择运算符号(+、-、*、/)");
                string op = Console.ReadLine();
                Console.WriteLine("请输入数字B");
                string numberB = Console.ReadLine();
                Operation oper;
                oper = OperationFactory.createOperate(op);
                oper.numberA = double.Parse(numberA);
                oper.numberB = double.Parse(numberB);
                double result = oper.GetResult();
                Console.WriteLine("运算结果是：" + result);
            }
            catch (Exception ex) {
                Console.WriteLine("输入有误:" + ex.ToString());
            }
            Console.ReadLine();
        }
    }
    //运算类
    public class Operation
    {
        private double _numberA = 0;
        private double _numberB = 0;
        public double numberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }
        public double numberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }

        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }

    //运算工厂类
    public class OperationFactory
    {
        public static Operation createOperate(string operate)
        {
            Operation op = null;
            switch (operate)
            {
                case "+":
                    op = new OperationAdd();
                    break;
                case "-":
                    op = new OperationSub();
                    break;
                case "*":
                    op = new OperationMul();
                    break;
                case "/":
                    op = new OperationDiv();
                    break;
            }
            return op;
        }
    }

    #region 计算方法
    public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = numberA + numberB;
            return result;
        }
    }

    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = numberA - numberB;
            return result;
        }
    }

    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = numberA * numberB;
            return result;
        }
    }

    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (numberB == 0)
                throw new Exception("除数不能为零！");
            result = numberA / numberB;
            return result;
        }
    }
    #endregion


}
